package com.heimdallr.rbac.user.web;

import com.heimdallr.rbac.permission.entity.GroupEntity;
import com.heimdallr.rbac.permission.entity.UserEntity;
import com.heimdallr.rbac.user.service.UserService;
import com.heimdallr.util.AppUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author user
 */
@Controller
public class UserController {

    private final static Logger logger = LoggerFactory.getLogger(UserController.class);
 
	@Autowired
	private UserService userService;

	@RequestMapping( value="/user/index")
	public String index(){
 		
		return "user/index";
	}
	
	@RequestMapping( value="/user/getList",method = RequestMethod.GET)
	public @ResponseBody
	ResponseEntity<List<UserEntity>> getList(@RequestParam String userName,
											 @RequestParam String groups,
											 @RequestParam String status){


        Map<String,Object> paraMap = new HashMap<>();
        paraMap.put("userName",userName);
        paraMap.put("groups",groups);
        paraMap.put("status",status);
 		List<UserEntity> list = userService.getUserList(paraMap);
        return new ResponseEntity<>(list, HttpStatus.OK);
 
	}

	@RequestMapping( value="/user/detail")
	public String detail(){

		return "user/detail";
	}

    @RequestMapping( value="/user/save",method = RequestMethod.POST)
    public @ResponseBody
    ResponseEntity<String> save(@RequestParam String username,
                                          @RequestParam String nickname,
                                             @RequestParam String oldPassword,
                                             @RequestParam String newPassword){

	    UserEntity userEntity = userService.getUserEntityByUserName(username);
	    if(userEntity==null){
	        //用户不存在
            return new ResponseEntity<>("1", HttpStatus.OK);
        }

        String encodePassword = "";
        try {
            encodePassword = AppUtils.md5Encode(AppUtils.md5Encode(oldPassword));
        } catch (Exception e) {
            logger.error("密码转换MD5失败！密码为：" + oldPassword, e);
        }

        if(encodePassword.equals(userEntity.getPassword())){
            //update userInfo
            try {
                String encodeNewPassword = AppUtils.md5Encode(AppUtils.md5Encode(newPassword));
                userService.updateUserEntity(username,nickname,encodeNewPassword);
            } catch (Exception e) {
                logger.error("密码转换MD5失败！密码为：" + oldPassword, e);
                return new ResponseEntity<>("3", HttpStatus.OK);
            }
        }else{
            //密码不正确
            return new ResponseEntity<>("2", HttpStatus.OK);
        }

        Subject currentUser = SecurityUtils.getSubject();
        if (null != currentUser) {
            Session session = currentUser.getSession();
            if (null != session) {
                session.setAttribute("currentNickname", nickname);
            }
        }

        return new ResponseEntity<>("0", HttpStatus.OK);
    }


	@RequestMapping( value="/user/getUserGroups",method = RequestMethod.GET)
	public @ResponseBody
	ResponseEntity<List<GroupEntity>> getUserGroups(){

		List<GroupEntity> list = userService.getAllUserGroup();
		return new ResponseEntity<>(list, HttpStatus.OK);
	}

    @RequestMapping( value="/user/editGroup",method = RequestMethod.GET)
    public @ResponseBody
	ResponseEntity<String> editGroup(@RequestParam String userId,
									 @RequestParam String groupId){

        String code ="0";
        if(userService.editUserGroup(userId,groupId)){
            code="1";
        }

        return new ResponseEntity<>(code, HttpStatus.OK);
    }
	
	 
}
